package com.glq1218.security.exception;

import com.alibaba.fastjson2.JSON;
import com.glq1218.enums.ResultEnum;
import com.glq1218.response.ResponseResult;
import com.glq1218.util.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义
 */
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        WebUtils.renderString(response, JSON.toJSONString(ResponseResult.error(ResultEnum.NO_PERMISSION)));
    }
}
